<?php
namespace app\index\domain;

class Term extends Base
{
    public function __construct($id)
    {
        if(is_array($id)){
            $this->_self = $self = $id;
            $this->_id   = $self['id'];
        }else{
            $this->_id      =   $id;
            $this->_self = D('Term')->getInfo($id);
        }
    }

    public function valueGet($key)
    {
        if($result=$this->getSelf($key))
            return $result;
        return null;
    }

    /**
     * 获取栏目下的内容
     */
    public function getContent()
    {
        if(isset($this->_content))
            return $this->_content;
        //if($content = new ContentList($this->_self['id']))
        if($content = Classes::get('ContentListById', $this->_self['id']))
            return $this->_content = $content;
    }

    /**
     * 获取栏目下的留言
     * 废弃
     */
    public function getForm()
    {
        if(isset($this->_form))
            return $this->_form;
        //if($content = new ContentList($this->_self['id']))
        if($content = Classes::get('FormList', $this->_self['id']))
            return $this->_form = $content;
    }

    /**
     * 获取栏目的父栏目
     */
    public function getParent()
    {
        if(isset($this->_parent))
            return $this->_parent;
        //if($parent = new Term($this->_self['parent_id'], 'id'))
        if($parent = Classes::get('TermById', $this->_self['parent_id']))
            return $this->_parent = $parent;
    }

    /**
     * 获取栏目的页面标题
     */
    public function getTitle()
    {
        if(isset($this->_title))
            return $this->_title;
        $option = Classes::get('Option');
        if(!empty($this['seo_title']))
            return $this->_title = $this['seo_title'];
        if(isset($option['title']))
            return $this->_title = $this['name'].'-'.$option['title'];
        else
            return $this->_title = $this['name'];
    }

    /**
     * 获取栏目路径
     */
    public function getAddress()
    {
        if(isset($this->_address))
            return $this->_address;
        if($address = Classes::get('Address', $this->_id))
            return $this->_address = $address;
    }

    /**
     * 获取栏目的子栏目列表
     */
    public function getChild()
    {
        if(isset($this->_child))
            return $this->_child;
        //if($child = new TermList($this->_id))
        if($child = Classes::get('TermListById', $this->_id))
            return $this->_child = $child;
    }

    /**
     * 获取栏目的所有子栏目的内容
     */
    public function getChildContent()
    {
        if(isset($this->_child_content))
            return $this->_child_content;
        //if($child = new TermList($this->_id))
        $childIdList = D('Term')->getAllChildId($this->_id);
        if($content = Classes::get('ContentList', $childIdList))
            return $this->_child_content = $content;
    }

    /**
     * 获取此栏目基本信息
     */
    public function getSelf($field)
    {
        if(!isset($this->_self[$field]))
            return null;
        return $this->_self[$field];
    }

    /**
     * 获取此栏目的地址
     */
    public function getUrl()
    {
        $url = U('/t/'.$this['flag']);
        //$url = '/term/'.$this['flag'].'.html';
        return $url;
    }
}

